#include "CCoroutineGroupImpl.h"
#include "CCoroutineImpl.h"

namespace TB
{
	CCoroutineGroupImpl::CCoroutineGroupImpl(const std::weak_ptr<CThreadPool>& poThreadPool)
	{
		if (poThreadPool.expired())
			m_poThreadPool = CThreadPool::Default();
		else
			m_poThreadPool = poThreadPool;
	}

	CCoroutineGroupImpl::~CCoroutineGroupImpl()
	{
	}

	void CCoroutineGroupImpl::Post(const std::function<void()>& rcbFunction)
	{
		UINT32 dwIdleCount = m_oIdleCoroutineQueue.GetCount();
		if (dwIdleCount != 0 && m_oIdleCount.load(std::memory_order_relaxed) != 0 && m_oTaskQueue.Push(rcbFunction) + dwIdleCount >= m_dwSize)
		{
			std::shared_ptr<CCoroutineImpl> poImpl;
			if (0 != m_oIdleCoroutineQueue.Pop(poImpl))
			{
				poImpl->Post(nullptr, true);
			}
		}
	}
}